#[derive(Debug)]
enum List {
    Cons(RefCell<i32>, Rc<List>),
    Nil,
}

use crate::List::{Cons, Nil};
use std::cell::RefCell;
use std::rc::Rc;

fn main() {
    let value = RefCell::new(5);

    let a = Rc::new(Cons(value, Rc::new(Nil)));
    let b = Cons(RefCell::new(3), Rc::clone(&a));
    let c = Cons(RefCell::new(4), Rc::clone(&a));

    // 通过 RefCell 安全地修改值
    if let Cons(value, _) = &*a {
        *value.borrow_mut() = 100;
    }
    // -- below fails
    // *((&*a).0.borrow_mut()) = 100;

    println!("a after = {a:?}");
    println!("b after = {b:?}");
    println!("c after = {c:?}");
}
